package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.activity.TwoDatePickActivity;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceEditOrderWorkpartAdapter;
import com.cloudsigns.factorymanager2.produce.bean.Produce_ProductBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.iview.IProductOrderEditView;
import com.cloudsigns.factorymanager2.produce.presenter.ProductOrderEditPresenter;
import com.cloudsigns.factorymanager2.project.iview.IProductDetailView;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;
import com.cloudsigns.factorymanager2.project.presenter.ProductDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class ProduceEditOrderActivity extends BaseActivity implements ItemRemoveListener, ItemClickListener, IProductOrderEditView, IProductDetailView {
    private String TAG = ProduceEditOrderActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    //    private RelativeLayout rl_right;
    private TextView tv_title;
    private TextView tv_product_name;
    private TextView tv_order_information;
    private ImageView img_production_cycle;
    private RelativeLayout rl_production_cycle;
    private TextView tv_production_cycle;
    private ImageView img_add_process;
    private ListViewForScrollView lv_add_process;
    private Button btn_submit;

    private Produce_ProductBean produce_productBean;

    private String beginDate, completeDate;

    private ProduceEditOrderWorkpartAdapter adapter;
    private List<WorkPartBean> workPartBeanList;
    private ProductOrderEditPresenter productOrderEditPresenter;

    private ProductDetailPresenter productDetailPresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_edit_order);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
//        rl_right = findViewById(R.id.rl_right);
        tv_title = findViewById(R.id.tv_title);
        tv_product_name = findViewById(R.id.tv_product_name);
        tv_order_information = findViewById(R.id.tv_order_information);
        img_production_cycle = findViewById(R.id.img_production_cycle);
        rl_production_cycle = findViewById(R.id.rl_production_cycle);
        tv_production_cycle = findViewById(R.id.tv_production_cycle);
        img_add_process = findViewById(R.id.img_add_process);
        lv_add_process = findViewById(R.id.lv_add_process);
        btn_submit = findViewById(R.id.btn_submit);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.edit_order));

        Intent intent = getIntent();
        if (null != intent) {
            produce_productBean = (Produce_ProductBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
        }

        productDetailPresenter = new ProductDetailPresenter(this);
        productDetailPresenter.getProductDetail();

        productOrderEditPresenter = new ProductOrderEditPresenter(this);

        tv_product_name.setText(produce_productBean.getProductName());


        workPartBeanList = produce_productBean.getWorkPartList();

        adapter = new ProduceEditOrderWorkpartAdapter(this, TAG);
        adapter.setData(workPartBeanList);
        adapter.setRemoveListener(this);
        adapter.setItemClickListener(this);
        lv_add_process.setAdapter(adapter);
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
//        rl_right.setOnClickListener(this);
        tv_order_information.setOnClickListener(this);
        img_production_cycle.setOnClickListener(this);
        img_add_process.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 编辑工序名称
//            case R.id.rl_right:
//                mIntent = new Intent(ProduceEditOrderActivity.this, ProduceEditWorkpartActivity.class);
//                startActivity(mIntent);
//                break;
            // 工单信息
            case R.id.tv_order_information:
                mIntent = new Intent(ProduceEditOrderActivity.this, ProduceReceiveOrderItemDetailActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_2, "ProduceEditOrderActivity");
                mIntent.putExtra("orderId", produce_productBean.getOrderId());
                mIntent.putExtra("productId", produce_productBean.getProductId());
                startActivity(mIntent);
                break;
            // 编辑生产周期
            case R.id.img_production_cycle:
                mIntent = new Intent(ProduceEditOrderActivity.this, TwoDatePickActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_2);
                break;
            // 添加工序
            case R.id.img_add_process:
                mIntent = new Intent(ProduceEditOrderActivity.this, ProduceAddWorkpartActivity.class);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
            // 确认编辑
            case R.id.btn_submit:
                if (TextUtils.isEmpty(beginDate) || TextUtils.isEmpty(completeDate)) {
                    showToast(getResources().getString(R.string.edit_produce_cycle));
                } else if (null == workPartBeanList || workPartBeanList.isEmpty()) {
                    showToast(getResources().getString(R.string.add_workpart));
                } else {
                    for (WorkPartBean pb : workPartBeanList) {
                        if (!pb.isEdit()) {
                            showToast("请编辑【" + pb.getWorkPartName() + "】的加工信息");
                            return;
                        }
                    }
                    for (WorkPartBean pb : workPartBeanList) {
                        if (pb.getWorkPartScheduleList().isEmpty()) {
                            showToast("请编辑【" + pb.getWorkPartName() + "】的加工计划");
                            return;
                        }
                    }
                    productOrderEditPresenter.productOrderEdit();
                }
                break;
        }
    }

    @Override
    public void itemClick(View view, int position) {
        Intent intent;
        switch (view.getId()) {
            // 编辑加工计划
            case R.id.img_date:
                WorkPartBean wpDate = workPartBeanList.get(position);
                if (!TextUtils.isEmpty(DateUtil.getYyyy_Mm_Dd(wpDate.getStartDate())) && !TextUtils.isEmpty(DateUtil.getYyyy_Mm_Dd(wpDate.getCompleteDate()))) {
                    intent = new Intent(ProduceEditOrderActivity.this, ProduceEditProcessPlanActivity.class);
                    intent.putExtra(ConstantUtil.KEY_1, wpDate);
                    startActivityForResult(intent, ConstantUtil.START_ACTIVITY_KEY_3);
                } else {
                    showToast(getResources().getString(R.string.edit_process_information_pl));
                }
                break;
            // 编辑加工信息
            case R.id.img_edit:
                WorkPartBean wpbDate = workPartBeanList.get(position);
                intent = new Intent(ProduceEditOrderActivity.this, ProduceEditProcessInformationActivity.class);
                intent.putExtra(ConstantUtil.KEY_1, wpbDate);
                startActivityForResult(intent, ConstantUtil.ADD_WORKPART_INFOMATION);
                break;
            // 加工信息
            case R.id.rl_process_information:
                WorkPartBean wp = workPartBeanList.get(position);
                intent = new Intent(ProduceEditOrderActivity.this, ProduceWorkpartInformationActivity.class);
                intent.putExtra(ConstantUtil.KEY_1, wp);
                startActivityForResult(intent, ConstantUtil.DELETE_WORKPART_INFOMATION);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            // 添加工序
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    workPartBeanList = (ArrayList<WorkPartBean>) data.getSerializableExtra(ConstantUtil.KEY_2);
                    adapter.setData(workPartBeanList);
                    adapter.notifyDataSetChanged();
                }
                break;
            // 编辑生产周期
            case ConstantUtil.START_ACTIVITY_KEY_2:
                if (data != null) {
                    rl_production_cycle.setVisibility(View.VISIBLE);
                    beginDate = data.getStringExtra(ConstantUtil.KEY_1);
                    completeDate = data.getStringExtra(ConstantUtil.KEY_2);
                    tv_production_cycle.setText(beginDate + "---" + completeDate);
                }
                break;
            // 编辑加工信息
            case ConstantUtil.ADD_WORKPART_INFOMATION:
                if (data != null) {
                    WorkPartBean workPartBean = (WorkPartBean) data.getSerializableExtra(ConstantUtil.WORKPARTBEAN);
                    for (int i = 0; i < workPartBeanList.size(); i++) {
                        if (workPartBeanList.get(i).getWorkPartName().equals(workPartBean.getWorkPartName())) {
//                            workPartBean.isEdit = true;
                            workPartBeanList.set(i, workPartBean);
                            adapter.setData(workPartBeanList);
                            adapter.notifyDataSetChanged();
                            break;
                        }
                    }
                }
                break;
            // 编辑加工计划
            case ConstantUtil.START_ACTIVITY_KEY_3:
                if (data != null) {
                    WorkPartBean workPartBean = (WorkPartBean) data.getSerializableExtra(ConstantUtil.KEY_1);
                    for (int i = 0; i < workPartBeanList.size(); i++) {
                        if (workPartBeanList.get(i).getWorkPartName().equals(workPartBean.getWorkPartName())) {
                            workPartBeanList.set(i, workPartBean);
                            adapter.setData(workPartBeanList);
                            adapter.notifyDataSetChanged();
                            break;
                        }
                    }
                }
                break;
            // 删除加工信息
            case ConstantUtil.DELETE_WORKPART_INFOMATION:
                if (data != null) {
                    WorkPartBean workPartBean = (WorkPartBean) data.getSerializableExtra(ConstantUtil.WORKPARTBEAN);
                    for (int i = 0; i < workPartBeanList.size(); i++) {
                        if (workPartBeanList.get(i).getWorkPartName().equals(workPartBean.getWorkPartName())) {
                            workPartBeanList.set(i, workPartBean);
                            adapter.setData(workPartBeanList);
                            adapter.notifyDataSetChanged();
                            break;
                        }
                    }
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void remove(int positon) {
        if (positon >= 0 && positon < workPartBeanList.size()) {
            workPartBeanList.remove(positon);
            adapter.setData(workPartBeanList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void remove(View view) {

    }

    @Override
    public long getBeginDate() {
        return DateUtil.getMillis(beginDate);
    }

    @Override
    public long getFinishDate() {
        return DateUtil.getMillis(completeDate);
    }

    @Override
    public String getOrderId() {
        return produce_productBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return produce_productBean.getProductId();
    }

    @Override
    public void getProductDetailSuccessed(Produce_ProductBean productBean) {
        if (null != productBean.getWorkPartList() && !productBean.getWorkPartList().isEmpty()) {
            rl_production_cycle.setVisibility(View.VISIBLE);
            tv_production_cycle.setText(DateUtil.getYyyy_Mm_Dd(productBean.getBeginDate()) + "---" + DateUtil.getYyyy_Mm_Dd(productBean.getFinishDate()));
            workPartBeanList.clear();
            workPartBeanList.addAll(productBean.getWorkPartList());
            adapter.setData(workPartBeanList);
            adapter.notifyDataSetChanged();
            lv_add_process.setAdapter(adapter);

//            btn_submit.setBackgroundResource(R.mipmap.btn_long_bg_gray);
        }
    }

    @Override
    public void getProductDetailFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public List<WorkPartBean> getWorkPartList() {
        return workPartBeanList;
    }

    @Override
    public void getProductOrderEditSuccessed() {
        showToast(getResources().getString(R.string.edit_success));
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, true);
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        finish();
    }

    @Override
    public void getProductOrderEditFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
